<template>
  <el-dialog v-el-drag-dialog :title="!form.appId?'新建短信模板':'编辑短信模板'" :visible.sync="visible" :append-to-body="appendToBody" :before-close="onClose">
    <el-form ref="templateForm" :model="form" :rules="rules" label-width="120px" :disabled="!hasLoaded">
      <el-card>
        <el-form-item label="渠道编码" prop="channelCode">
          <el-select v-model="form.channelCode">
            <el-option
              v-for="item in optionOfType"
              :key="item.dictLabel + '_' + item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="短信API模板编号" prop="templateId">
          <el-input v-model="form.templateId" />
        </el-form-item>
        <el-form-item label="模板编号" prop="templateCode">
          <el-input v-model="form.templateCode" />
        </el-form-item>
        <el-form-item label="模板名称" prop="templateName">
          <el-input v-model="form.templateName" />
        </el-form-item>
        <el-form-item label="模板内容" prop="content">
          <el-input type="textarea" v-model="form.content" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status">
            <el-option
              v-for="item in optionOfStatus"
              :key="item.dictCode"
              :label="item.dictLabel"
              :value="item.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            :rows="3"
          />
        </el-form-item>
      </el-card>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button v-if="hasLoaded" type="primary" icon="el-icon-check" @click="onSubmit">确 定</el-button>
      <el-button icon="el-icon-close" @click="onClose">关 闭</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { smsTemplateLoad, smsTemplateSave } from '@/api/tool/sms'
import { dictGroup } from '@/api/system/dict'

export default {
  name: 'SmsTemplateFormDialog',
  data() {
    return {
      visible: false,
      appendToBody: false,
      hasLoaded: false,
      form: {
        id: undefined,
        channelCode: undefined,
        templateId: undefined,
        templateCode: undefined,
        templateName: undefined,
        content: undefined,
        status: '0',
        remark: undefined
      },
      rules: {
        channelCode: [{ required: true, message: '请选择短信渠道' }],
        templateId: [{ required: true, message: '短信API模板编号不能为空' }],
        templateCode: [{ required: true, message: '模板编码不能为空' }],
        templateName: [{ required: true, message: '模板名称不能为空' }],
        content: [{ required: true, message: '模板内容不能为空' }],
      },
      optionOfType: [],
      optionOfStatus: []
    }
  },
  methods: {
    init(id) {
      this.hasLoaded = false
      this.form.id = id || null
      this.visible = true
      this.$nextTick(() => {
        this.loadForm(id)
        this.getDictData()
      })
    },
    loadForm(id) {
      if (id) {
        smsTemplateLoad({ 'id': id }).then(response => {
          this.form = this.$api.objectUpdate(this.form, response.param)
        }).catch(() => {})
      }

      // 判断当前用户权限
      this.hasLoaded = this.$perm.hasPermissionOr(['sms:template:create'])
    },
    getDictData() {
      dictGroup({ client: 'SmsChannelType' }).then(response => {
        this.optionOfType = response
      }).catch(e => {})
      this.$dictGroup('sys_normal_disable').then(response => {
        this.optionOfStatus = response
      }).catch(e => {})
    },
    onSubmit() {
      this.$refs.templateForm.validate(valid => {
        if (valid) {
          smsTemplateSave(this.form).then(response => {
            this.$modal.msgSuccess(response.msg)
            this.$emit('confirmForm')
            this.onClose()
          }).catch(() => {})
        }
      })
    },
    onClose() {
      this.$refs.templateForm.resetFields()
      this.visible = false
    }
  }
}
</script>
